package com.android.gufengmall.adapter;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.android.gufengmall.R;
import com.android.gufengmall.bean.GoodsBean;

import java.util.List;

/**
 * 主页的古文化知识适配器
 */
public class MallShopAdapter extends RecyclerView.Adapter<MallShopAdapter.MallShopViewHolder> {

    private final static String TAG = "MallShopAdapter";

    private Context mContext;
    private List<GoodsBean> mGoodsBeans;
    private MallShopListener mListener;

    public MallShopAdapter(Context context, List<GoodsBean> goodsBeanList) {
        mContext = context;
        mGoodsBeans = goodsBeanList;
    }

    @NonNull
    @Override
    public MallShopViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new MallShopViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_mall_shop,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull MallShopViewHolder holder, int position) {
        GoodsBean goodsBean = mGoodsBeans.get(position);

        holder.mImgShop.setImageBitmap(BitmapFactory.decodeFile(goodsBean.getGoods_imgShop()));
        holder.mShopTitle.setText(goodsBean.getGoods_name());
        holder.mShopInfo.setText(goodsBean.getGoods_info());
        holder.mShopPrice.setText(String.valueOf(goodsBean.getGoods_price()));
        holder.itemView.setOnClickListener(v -> {
            mListener.onMallShopClick(goodsBean.getGoods_name(),position);
        });
    }

    @Override
    public int getItemCount() {
        return mGoodsBeans.size();
    }

    public class MallShopViewHolder extends RecyclerView.ViewHolder {

        private final ImageView mImgShop;
        private final TextView mShopTitle;
        private final TextView mShopInfo;
        private final TextView mShopPrice;

        public MallShopViewHolder(@NonNull View itemView) {
            super(itemView);

            mImgShop = itemView.findViewById(R.id.img_shop);
            mShopTitle = itemView.findViewById(R.id.tv_shop_title);
            mShopInfo = itemView.findViewById(R.id.tv_shop_info);
            mShopPrice = itemView.findViewById(R.id.tv_shop_price);


        }
    }

    public void setOnMallShopListener(MallShopListener mallShopListener) {
        mListener = mallShopListener;
    }

    public interface MallShopListener{
        void onMallShopClick(String goodsName,int position);
    }
}
